﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using uTrade.Core;
using uTrade.Common;

namespace uTrade.Controls
{
    public class CandleLine : DrawObjBase
    {
        public DataSeries Open;
        public DataSeries Close;
        public DataSeries High;
        public DataSeries Low;

        protected override int TotalItemCount { get => Open.Count; }

        //绘制Candle线
        protected override void DrawObj(DrawingContext dc)
        {
            // 1.Get Max and min
            double highest = 0;
            double lowest = double.MaxValue;

            var h = High.Highest(drawItemStartIndex, drawItemCount);
            if (h > highest)
            {
                highest = h;
            }
            var l = Low.Lowest(drawItemStartIndex, drawItemCount);
            if (l < lowest)
            {
                lowest = l;
            }

            //uLog.LogInfo(string.Format("drawItemStartIndex:{0},drawItemCount{1}", drawItemStartIndex, drawItemCount));
            //uLog.LogInfo(string.Format("highest:{0},lowest:{1}", highest, lowest));
            //uLog.LogInfo(string.Format("highestindex:{0},lowestindex:{1}", h.Item2, l.Item2));
            //uLog.LogInfo(string.Format("ymax:{0},ymin:{1}", ymax, ymix));
            var opens = Open;
            var closes = Close;
            var highs = High;
            var lows = Low;
            if (opens == null || closes == null || highs == null || lows == null
                || opens.Count == 0
                || opens.Count != closes.Count || closes.Count != highs.Count || highs.Count != lows.Count)
            {
                return;
            }

            {
                //uLog.LogError(string.Format("ChartStartX:{0},ChartStartY:{1},ChartHeight{2},ChartWidth{3}", ChartStartX, ChartStartY, ChartHeight, ChartWidth));
                //uLog.LogError(string.Format("drawItemStartIndex:{0}", drawItemStartIndex));
                //var hOpen = MathUtil.GetHighest(Open.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var hClose = MathUtil.GetHighest(Close.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var hHigh = MathUtil.GetHighest(High.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var hLow = MathUtil.GetHighest(Low.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var lOpen = MathUtil.GetLowest(Open.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var lClose = MathUtil.GetLowest(Close.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var lHigh = MathUtil.GetLowest(High.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);
                //var lLow = MathUtil.GetLowest(Low.ToArray(), drawItemStartIndex, drawItemStartIndex + drawItemCount);

                //uLog.LogError(string.Format("Open=max:{0},min:{1}", hOpen, lOpen));
                //uLog.LogError(string.Format("Close=max:{0},min:{1}", hClose, lClose));
                //uLog.LogError(string.Format("High=max:{0},min:{1}", hHigh, lHigh));
                //uLog.LogError(string.Format("Low=max:{0},min:{1}", hLow, lLow));
            }

            // 2.绘制可视区域的K线
            for (int i = 0; i < drawItemCount; ++i)
            {
                int itemIndex = drawItemStartIndex + i;
                double xoffset = (int)(i * (itemWidth + itemSpace)) + 0.5 + ChartStartX;
                double yTop = (int)((highest - highs[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY;

                double yBottom = (int)((highest - lows[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY;
                double yOpen = (int)((highest - opens[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY;
                double yClose = (int)((highest - closes[itemIndex]) / (highest - lowest) * ChartHeight) + 0.5 + ChartStartY;
                double bodyBottom = yOpen;
                double bodyTop = yClose;

                //uLog.LogInfo(string.Format("yTop:{0},itemIndex:{1},highval:{2}", yTop, itemIndex, highs[itemIndex]));

                var brush = Brushes.Red;
                var blackpen = new Pen(Brushes.Black, 1);

                if (opens[itemIndex] > closes[itemIndex])
                {
                    bodyTop = yOpen;
                    bodyBottom = yClose;
                    brush = Brushes.Green;
                }

                //draw top vertical line
                dc.DrawLine(blackpen, new Point(xoffset + itemWidth / 2, yTop), new Point(xoffset + itemWidth / 2, bodyTop));

                //draw kline body
                double bodyHeight = bodyBottom - bodyTop;
                dc.DrawRectangle(brush, blackpen, new Rect(xoffset, bodyTop, itemWidth, bodyHeight));
                //draw bottom line.
                dc.DrawLine(blackpen, new Point(xoffset + itemWidth / 2, bodyBottom), new Point(xoffset + itemWidth / 2, yBottom));
            }
        }
    }
}